package cn.hsp.tiktok

import android.graphics.Bitmap
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.MediaController
import android.widget.VideoView
import androidx.fragment.app.Fragment

class HomeVideoFragment(var url: String) : Fragment() {
    private lateinit var mImageView: ImageView
    private var mBitmap: Bitmap? = null
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = LayoutInflater.from(context).inflate(R.layout.item_video, null)
//        val view = layoutInflater.inflate(R.layout.item_video_view, null)
//        val videoView = view.findViewById<VideoView>(R.id.videoView)
//        val mc = MediaController(context)
//        mc.visibility = View.INVISIBLE
//        videoView.setMediaController(mc)
//        playVideoFromRaw(R.raw.video1, videoView)
        return view
    }

    private fun playVideoFromRaw(rawFileId: Int, videoView: VideoView) =
        playVideo("android.resource://${context?.packageName}/$rawFileId", videoView)

    private fun playVideo(filePath: String, videoView: VideoView) {
        val mediaController = MediaController(context)
        videoView.setMediaController(mediaController)
        videoView.setVideoURI(Uri.parse(filePath))
        videoView.start()
    }

    fun getImage() = mBitmap
}